obfuscate map key (#740)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Wed, 20 Oct 2021 13:14:17 +0000 (07:14 -0600)
committerGitHub <noreply@github.com>
Wed, 20 Oct 2021 13:14:17 +0000 (07:14 -0600)
* obfuscate map key.

* keep js debug turned off.

gui/gmapbase.html
gui/map.cc
gui/map.h

index d0be535b32e5cec2b96af4ecb195460314c8c23d..af37b8168d4fc27b8faeb02bba21de1e26448e4b 100644 (file)
@@ -3,7 +3,7 @@
   <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
     <title>Google Maps JavaScript</title>
-    <script src="https://maps.googleapis.com/maps/api/js?v=3&amp;key=AIzaSyBx9CSaJaOdcC5SV2LmaZZB6EOxFm1hLH4"></script>
+    <script src="https://maps.googleapis.com/maps/api/js?v=3&amp;key=APIKEY"></script>
     <script src="qrc:///qtwebchannel/qwebchannel.js"></script>
     <script>
 /*jslint
index e3034bbf14d798ac0ff4c534028353e414a471ac..c8ac0d76dc73e9547f79399d9e70088f5e52c01f 100644 (file)
 //------------------------------------------------------------------------
 #include "map.h"
 
-#include <QNetworkRequest>
-#include <QMessageBox>
-#include <QNetworkAccessManager>
-#include <QWebEngineView>
-#include <QWebEnginePage>
-#include <QWebChannel>
-#include <QApplication>
-#include <QCursor>
-#include <QFile>
-#include <QTextStream>
-
-#include <math.h>
-#include <string>
-#include <vector>
-#include "appname.h"
+#include <QApplication>           // for QApplication
+#include <QChar>                  // for QChar, operator!=
+#include <QCursor>                // for QCursor
+#include <QFile>                  // for QFile
+#include <QIODevice>              // for QIODevice, operator|, QIODevice::ReadOnly, QIODevice::Truncate, QIODevice::WriteOnly
+#include <QLatin1String>          // for QLatin1String
+#include <QMessageBox>            // for QMessageBox
+#include <QNetworkAccessManager>  // for QNetworkAccessManager
+#include <QStringLiteral>         // for QStringLiteral
+#include <QUrl>                   // for QUrl
+#include <QWebChannel>            // for QWebChannel
+#include <QWebEnginePage>         // for QWebEnginePage
+#include <QWebEngineView>         // for QWebEngineView
+#include <Qt>                     // for WaitCursor
+#include <QtGlobal>               // for foreach
+
+#include <algorithm>              // for max
+#include <string>                 // for string
+#include <vector>                 // for vector
+
+#include "appname.h"              // for appName
+#include "gpx.h"                  // for GpxRoute, GpxTrack, GpxWaypoint, Gpx, GpxRoutePoint, GpxTrackPoint, GpxTrackSegment
+#include "latlng.h"               // for LatLng
+
 
 using std::string;
 using std::vector;
@@ -83,10 +92,28 @@ Map::Map(QWidget* parent,
   // 2. In the Qt resource system.  This is useful if the resource was compiled
   //    into the executable.
   QString baseFile =  QApplication::applicationDirPath() + "/gmapbase.html";
+  QString fileName;
+  QUrl baseUrl;
   if (QFile(baseFile).exists()) {
-    this->load(QUrl::fromLocalFile(baseFile));
+    fileName = baseFile;
+    baseUrl = QUrl::fromLocalFile(baseFile);
   } else if (QFile(":/gmapbase.html").exists()) {
-    this->load(QUrl("qrc:///gmapbase.html"));
+    fileName = ":/gmapbase.html";
+    baseUrl = QUrl("qrc:///gmapbase.html");
+  }
+
+  if (!fileName.isEmpty()) {
+    QFile htmlFile(fileName);
+    if (htmlFile.open(QIODevice::ReadOnly)) {
+      QByteArray content = htmlFile.readAll();
+      htmlFile.close();
+      const QByteArray encodedKey = QByteArray::fromBase64("Qkp7YlR6Q3k6RFRiS2JQZWRENlRXM01uYltbQzdGUHlHbjJpTUk1");
+      content.replace("APIKEY", decodeKey(encodedKey));
+      this->setContent(content, "text/html;charset=UTF-8", baseUrl);
+    } else {
+      QMessageBox::critical(nullptr, appName,
+                            tr("Error opening \"gmapbase.html\" file.  Check installation"));
+    }
   } else {
     QMessageBox::critical(nullptr, appName,
                           tr("Missing \"gmapbase.html\" file.  Check installation"));
@@ -94,13 +121,33 @@ Map::Map(QWidget* parent,
 
 #ifdef DEBUG_JS_GENERATION
   dbgdata_ = new QFile("mapdebug.js");
-  if (dbgdata_->open(QFile::WriteOnly | QIODevice::Truncate)) {
+  if (dbgdata_->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
     dbgout_ = new QTextStream(dbgdata_);
   }
 #endif
 
 }
 
+//------------------------------------------------------------------------
+QByteArray Map::encodeKey(const QByteArray& key)
+{
+  QByteArray rv;
+  for (const auto c: key) {
+    rv.append(c+1);
+  }
+  return rv;
+}
+
+//------------------------------------------------------------------------
+QByteArray Map::decodeKey(const QByteArray& key)
+{
+  QByteArray rv;
+  for (const auto c: key) {
+    rv.append(c-1);
+  }
+  return rv;
+}
+
 //------------------------------------------------------------------------
 Map::~Map()
 {
index 3166ef7b8f3ebcfcea07e629a8ad44b91ba61bcf..1ea743aabe7b41199008df60a2714986dce52be3 100644 (file)
--- a/gui/map.h
+++ b/gui/map.h
 //------------------------------------------------------------------------
 #ifndef MAP_H
 #define MAP_H
-#include <QWebEngineView>
-#include <QPlainTextEdit>
-#include <QElapsedTimer>
-#include <QFile>
-#include <QTextStream>
-#include "gpx.h"
 
-//#define DEBUG_JS_GENERATION
+#include <QByteArray>             // for QByteArray
+#include <QElapsedTimer>          // for QElapsedTimer
+#include <QFile>                  // for QFile
+#include <QList>                  // for QList
+#include <QNetworkAccessManager>  // for QNetworkAccessManager
+#include <QObject>                // for QObject, Q_OBJECT, emit, signals, slots
+#include <QPlainTextEdit>         // for QPlainTextEdit
+#include <QResizeEvent>           // for QResizeEvent
+#include <QString>                // for QString
+#include <QStringList>            // for QStringList
+#include <QTextStream>            // for QTextStream
+#include <QWebEngineView>         // for QWebEngineView
+#include <QWidget>                // for QWidget
+
+#include "gpx.h"                  // for Gpx, GpxRoute, GpxTrack, GpxWaypoint
+#include "latlng.h"               // for LatLng
+
 
-class QNetworkAccessManager;
+//#define DEBUG_JS_GENERATION
 
 
 class MarkerClicker: public QObject
@@ -91,6 +101,10 @@ public slots:
 
   void logTime(const QString&);
 
+private:
+  QByteArray encodeKey(const QByteArray& key);
+  QByteArray decodeKey(const QByteArray& key);
+
 signals:
   void waypointClicked(int i);
   void trackClicked(int i);